package br.com.poc.pitagorasnumero.model.bo.impl;

import java.util.ArrayList;
import java.util.List;

import br.com.poc.pitagorasnumero.model.bo.INameBO;
import br.com.poc.pitagorasnumero.type.TablePitagoricaType;
import br.com.poc.pitagorasnumero.util.ExpressionNumberPitagoras;
import br.com.poc.pitagorasnumero.util.StringUtil;

/**
 * The Class TabelaPitagorasBO.
 * 
 * @author irlan.gomes
 */
public class NameBO implements INameBO {

	/** The calculation manager. */
	private static CalculationNumberManager calcManager;

	static {
		calcManager = new CalculationNumberManager();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.model.bo.ITablePitagorasBO#doNumberExpression
	 * (java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberExpression(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] letterName = ExpressionNumberPitagoras
				.getOnlyLetter(nameValidated);

		final int letterCalculated = calcManager.calculateLetters(letterName);

		return letterCalculated;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberMotivation(java
	 * .lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberMotivation(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] letterName = ExpressionNumberPitagoras
				.getOnyVowel(nameValidated);

		final int letterCalculated = calcManager.calculateLetters(letterName);

		return letterCalculated;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.model.bo.ITablePitagorasBO#doNumberPersonality
	 * (java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberPersonality(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] letterName = ExpressionNumberPitagoras
				.getOnlyConsonants(nameValidated);

		final int letterCalculated = calcManager.calculateLetters(letterName);

		return letterCalculated;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberKey(java.lang
	 * .String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberKey(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] tuple = ExpressionNumberPitagoras
				.getSeparatorTuple(nameValidated);

		final String[] letterName = ExpressionNumberPitagoras
				.getOnlyLetter(tuple[0]);

		final int letterCalculated = calcManager.calculateLetters(letterName);

		return letterCalculated;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberCornerstone(java
	 * .lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberCornerstone(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] letterName = ExpressionNumberPitagoras
				.getOnlyLetter(nameValidated);

		final TablePitagoricaType letterValue = TablePitagoricaType
				.getValueLetter(letterName[1]);

		return letterValue.getValueLetter();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberInstinctiveDesire
	 * (java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberInstinctiveDesire(final String name) {

		final String nameValidated = calcManager.validateName(name);

		final String[] vowel = ExpressionNumberPitagoras
				.getOnyVowel(nameValidated);

		String firstVowel = "";

		for (int i = 0; i < vowel.length; i++) {
			if (StringUtil.notEmpty(vowel[i]) && firstVowel == "") {
				firstVowel = vowel[i];
			}
		}

		final TablePitagoricaType valueLetter = TablePitagoricaType
				.getValueLetter(firstVowel);

		return valueLetter.getValueLetter();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberVocationalHealth
	 * (java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doNumberVocationalHealth(final String name) {
		final String nameValidated = calcManager.validateName(name);

		final String[] tuple = ExpressionNumberPitagoras
				.getSeparatorTuple(nameValidated);

		final int surname = tuple.length - 1;
		final String[] letter = ExpressionNumberPitagoras
				.getOnlyLetter(tuple[surname]);

		final int letterCalculated = calcManager.calculateLetters(letter);

		return letterCalculated;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberKarma(java.lang
	 * .String)
	 */
	/** {@inheritDoc} **/
	@Override
	public List<Integer> doNumberKarma(final String name) {

		final List<Integer> numKarmaList = new ArrayList<Integer>();

		final String nameValidated = calcManager.validateName(name);

		final String[] letter = ExpressionNumberPitagoras
				.getOnlyLetter(nameValidated);

		final List<TablePitagoricaType> letterConverted = calcManager
				.convertLetterNumber(letter);

		for (final TablePitagoricaType valuePitagoras : TablePitagoricaType
				.values()) {

			if (valuePitagoras != TablePitagoricaType.ERROR) {

				int counterNumberNotExist = 0;
				for (final TablePitagoricaType valueConverted : letterConverted) {

					if (valuePitagoras.getValueLetter() != valueConverted
							.getValueLetter()) {
						counterNumberNotExist++;
					}

					if (counterNumberNotExist == letterConverted.size()) {
						numKarmaList.add(valuePitagoras.getValueLetter());
					}
				}
			}
		}

		return numKarmaList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doNumberTrendsHidden(
	 * java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public List<Integer> doNumberTrendsHidden(final String name) {
		final List<Integer> numMoreThree = new ArrayList<Integer>();

		final String nameValidated = calcManager.validateName(name);

		final String[] letter = ExpressionNumberPitagoras
				.getOnlyLetter(nameValidated);

		final List<TablePitagoricaType> nameConverted = calcManager
				.convertLetterNumber(letter);

		int counterNumberExist;

		for (final TablePitagoricaType valuePitagoras : TablePitagoricaType
				.values()) {

			counterNumberExist = 0;

			for (final TablePitagoricaType valueConverted : nameConverted) {

				if (valueConverted != TablePitagoricaType.ERROR
						&& valuePitagoras.getValueLetter() == valueConverted
								.getValueLetter()) {
					counterNumberExist++;
				}

				if (counterNumberExist == 4) {
					counterNumberExist = 0;
					numMoreThree.add(valuePitagoras.getValueLetter());
				}
			}

		}

		return numMoreThree;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.poc.pitagorasnumero.bo.ITablePitagorasBO#doAnswerSubconscious(
	 * java.lang.String)
	 */
	/** {@inheritDoc} **/
	@Override
	public int doAnswerSubconscious(final String name) {
		final List<Integer> numberKarma = doNumberKarma(name);
		int result = 0;

		if (!numberKarma.isEmpty()) {
			result = 9 - numberKarma.size();
		}

		return result;
	}

}
